<?php

use App\Models\Cell;
use App\Models\Column;
use App\Models\Project;
use App\Models\Row;
use App\Table\Constant\Column as CColumn;
use App\Table\Constant\Project as CProject;
use App\Table\Constant\Row as CRow;
use Illuminate\Database\Seeder;
use Faker\Generator as Faker;

class ProjectSeeder extends Seeder
{
    /**
     * @var Faker $faker
     */
    protected $faker;

    function __construct()
    {
        $this->faker = app(Faker::class);
    }
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $project = Project::create([
            'user_id' => 0,
            'template' => CProject::TEMPLATE_YES,
            'system' => CProject::SYSTEM_YES,
            'status' => CProject::STATUS_NORMAL,
            'title' => '默认模板',
        ]);

        $columnTaskName = Column::create([
            'project_id' => $project->id,
            'prev_id' => 0,
            'next_id' => 0,
            'title' => '任务名称',
            'description' => '',
            'frozen' => CColumn::FROZEN_NO,
            'hidden' => CColumn::HIDDEN_NO,
            'type' => CColumn::TYPE_TEXT,
        ]);

        $columnRemark = Column::create([
            'project_id' => $project->id,
            'prev_id' => $columnTaskName->id,
            'next_id' => 0,
            'title' => '备注',
            'description' => '',
            'frozen' => CColumn::FROZEN_NO,
            'hidden' => CColumn::HIDDEN_NO,
            'type' => CColumn::TYPE_TEXT,
        ]);

        $columnProgress = Column::create([
            'project_id' => $project->id,
            'prev_id' => $columnRemark->id,
            'next_id' => 0,
            'title' => '进度',
            'description' => '',
            'frozen' => CColumn::FROZEN_NO,
            'hidden' => CColumn::HIDDEN_NO,
            'type' => CColumn::TYPE_NUMBER,
        ]);

        $columnMaster = Column::create([
            'project_id' => $project->id,
            'prev_id' => $columnProgress->id,
            'next_id' => 0,
            'title' => '负责人',
            'description' => '',
            'frozen' => CColumn::FROZEN_NO,
            'hidden' => CColumn::HIDDEN_NO,
            'type' => CColumn::TYPE_USER,
        ]);

        $columnPredict = Column::create([
            'project_id' => $project->id,
            'prev_id' => $columnMaster->id,
            'next_id' => 0,
            'title' => '预估工时（人/日）',
            'description' => '',
            'frozen' => CColumn::FROZEN_NO,
            'hidden' => CColumn::HIDDEN_NO,
            'type' => CColumn::TYPE_NUMBER,
        ]);

        $columnActual = Column::create([
            'project_id' => $project->id,
            'prev_id' => $columnPredict->id,
            'next_id' => 0,
            'title' => '工时（人/日）',
            'description' => '',
            'frozen' => CColumn::FROZEN_NO,
            'hidden' => CColumn::HIDDEN_NO,
            'type' => CColumn::TYPE_NUMBER,
        ]);

        $columnStart = Column::create([
            'project_id' => $project->id,
            'prev_id' => $columnActual->id,
            'next_id' => 0,
            'title' => '开始时间',
            'description' => '',
            'frozen' => CColumn::FROZEN_NO,
            'hidden' => CColumn::HIDDEN_NO,
            'type' => CColumn::TYPE_DATE,
        ]);

        $columnEnd = Column::create([
            'project_id' => $project->id,
            'prev_id' => $columnStart->id,
            'next_id' => 0,
            'title' => '结束时间',
            'description' => '',
            'frozen' => CColumn::FROZEN_NO,
            'hidden' => CColumn::HIDDEN_NO,
            'type' => CColumn::TYPE_DATE,
        ]);

        $columnActualDone = Column::create([
            'project_id' => $project->id,
            'prev_id' => $columnEnd->id,
            'next_id' => 0,
            'title' => '实际完成时间',
            'description' => '',
            'frozen' => CColumn::FROZEN_NO,
            'hidden' => CColumn::HIDDEN_NO,
            'type' => CColumn::TYPE_DATE,
        ]);

        $columnDelay = Column::create([
            'project_id' => $project->id,
            'prev_id' => $columnActualDone->id,
            'next_id' => 0,
            'title' => '延期时间',
            'description' => '',
            'frozen' => CColumn::FROZEN_NO,
            'hidden' => CColumn::HIDDEN_NO,
            'type' => CColumn::TYPE_NUMBER,
        ]);

        $columnDelayReason = Column::create([
            'project_id' => $project->id,
            'prev_id' => $columnDelay->id,
            'next_id' => 0,
            'title' => '延期原因',
            'description' => '',
            'frozen' => CColumn::FROZEN_NO,
            'hidden' => CColumn::HIDDEN_NO,
            'type' => CColumn::TYPE_TEXT,
        ]);

        $row = null;
        for ($i = 0; $i < 20; $i ++) {
            $row = Row::create([
                'project_id' => $project->id,
                'prev_id' => is_null($row) ? 0 : $row->id,
                'next_id' => 0,
                'locked' => CRow::LOCKED_NO,
                'level' => 0
            ]);

            $this->fakeCell($project, $columnTaskName, $row);
            $this->fakeCell($project, $columnRemark, $row);
            $this->fakeCell($project, $columnProgress, $row);
            $this->fakeCell($project, $columnMaster, $row);
            $this->fakeCell($project, $columnPredict, $row);
            $this->fakeCell($project, $columnActual, $row);
            $this->fakeCell($project, $columnStart, $row);
            $this->fakeCell($project, $columnEnd, $row);
            $this->fakeCell($project, $columnActualDone, $row);
            $this->fakeCell($project, $columnDelay, $row);
            $this->fakeCell($project, $columnDelayReason, $row);
        }
    }

    protected function fakeCell(Project $project, Column $column, Row $row)
    {
        switch($column->type) {
            case CColumn::TYPE_TEXT:
                $content = $this->faker->text();
            break;
            case CColumn::TYPE_NUMBER;
                $content = $this->faker->randomNumber();
            break;
            case CColumn::TYPE_USER:
                $content = 1;
            break;
            case CColumn::TYPE_DATE:
                $content = $this->faker->date();
            break;
            case CColumn::TYPE_DATETIME:
                $content = $this->faker->dateTime;
            break;
            default:
            $content = '';
            break;
        }

        Cell::create([
            'project_id' => $project->id,
            'column_id' => $column->id,
            'row_id' => $row->id,
            'content' => $content,
        ]);
    }
}
